import path from 'path';
import { Tray, Menu, app, BrowserWindow } from 'electron';

let tray: Tray;

export function setUpTray(window: BrowserWindow) {
  const RESOURCES_PATH = app.isPackaged
    ? path.join(process.resourcesPath, 'assets')
    : path.join(__dirname, '../../assets');

  const getAssetPath = (...paths: string[]): string => {
    return path.join(RESOURCES_PATH, ...paths);
  };

  const icon = getAssetPath('tray_icon.png');

  tray = new Tray(icon);

  tray.setToolTip('工程数字化平台文档同步客户端');

  tray.setContextMenu(
    Menu.buildFromTemplate([
      {
        label: '退出',
        click: () => {
          window.destroy();
          app.quit();
        },
      },
    ])
  );

  tray.on('click', () => {
    window.restore();
  });
}

export function destroyTray() {
  tray.destroy();
}
